package com.qj.platform.pay.controller;

import com.qj.base.commons.exception.CodeDefinition;
import com.qj.base.commons.utils.RSA;
import com.qj.base.controller.BaseController;
import com.qj.base.domain.ResultTemplate;
import com.qj.platform.pay.commons.execption.SdsException;
import com.qj.platform.pay.core.domain.Operator;
import com.qj.platform.pay.core.service.OperatorService;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;
import java.util.Map;

/**
 * Created by hezhijie on 2016/11/28.
 */
@RestController
@RequestMapping("/operator")
public class OperatorController extends BaseController{

    @Autowired
    private OperatorService operatorService;

    @RequestMapping("/login")
    public ResultTemplate login(String name,String pwd){
        if(StringUtils.isEmpty(name)||StringUtils.isEmpty(pwd)){
            throw new SdsException(CodeDefinition.ILLEGAL_ARGUMENT_VALUE,"请填写用户名或者密码");
        }
        Operator operator=operatorService.login(name, pwd);
        Map<String,Object> objectMap=new HashMap<>();
        objectMap.put("operator",operator);
        objectMap.put("token", RSA.getInstance().encrypt(String.format("OPERATOR#%s#%s#%s",operator.getId(),operator.getName(),System.currentTimeMillis())));
        return getSuccessTemplate(objectMap);
    }
}
